package cn.dyp.com.getsensordata.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.dyp.com.getsensordata.R;
import cn.dyp.com.getsensordata.bean.SensorBean;

/**
 * 自定义传感器数据适配器
 * 
 * @author asus
 */

public class SensorGridAdapter extends ArrayAdapter<SensorBean> {
	/**
	 * 视图封装类
	 * 
	 * @author asus
	 *
	 */
	private static class ViewHolder {
		LinearLayout mBgLayout;// 背景布局
		TextView mNameTV; // 显示传感器名称
		TextView mStautsTV;// 显示传感器状态
		TextView mSetValeuTV;// 显示传感器的阀值
		TextView mValueTV;// 显示传感器的值
	}

	// 传感器值的集合
	private List<SensorBean> mDataArray;
	private Context mContext;
	private final LayoutInflater mInflater;

	/**
	 * 构造函数
	 * 
	 * @param context
	 *            上下文对象
	 * @param array
	 *            数据源
	 */
	public SensorGridAdapter(Context context, List<SensorBean> array) {
		super(context, 0, array);
		mContext = context;
		mDataArray = array;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 获取数据源长度
	 * 
	 * @see ArrayAdapter#getCount()
	 */
	public int getCount() {
		return mDataArray.size();
	}

	/**
	 * 获取单个Item的位置
	 * 
	 * @see ArrayAdapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * 获取单个Item的数据源
	 * 
	 * @see ArrayAdapter#getItem(int)
	 */
	@Override
	public SensorBean getItem(int position) {
		return mDataArray.get(position);
	}

	/**
	 * 自定义视图
	 * 
	 * @see ArrayAdapter#getView(int, View,
	 *      ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		try {
			SensorBean itemInfo = (SensorBean) mDataArray.get(position);
			ViewHolder holder = null;
			if (convertView == null) {

				convertView = mInflater.inflate(R.layout.sensor_grid_item,
						parent, false);
				holder = new ViewHolder();
				// 界面初始化
				holder.mBgLayout = (LinearLayout) convertView
						.findViewById(R.id.sensor_item_layout);
				holder.mNameTV = (TextView) convertView
						.findViewById(R.id.sensor_name_text);
				holder.mStautsTV = (TextView) convertView
						.findViewById(R.id.status_text);
				holder.mSetValeuTV = (TextView) convertView
						.findViewById(R.id.set_value_text);
				holder.mValueTV = (TextView) convertView
						.findViewById(R.id.sensor_value_text);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			if (itemInfo != null) {
				holder.mNameTV.setText(itemInfo.getName());
				holder.mValueTV.setText("" + itemInfo.getValue());

				// 显示设定值内容
				String setStr = mContext.getString(R.string.set_value);
				final int invalidMin = Integer.MIN_VALUE;
				int minV = itemInfo.getMinValue();
				int maxV = itemInfo.getMaxValue();
				if (minV > invalidMin && maxV > invalidMin) {
					setStr += minV + "~" + maxV;
				} else if (minV > invalidMin) {
					setStr += ">" + minV;
				} else if (maxV > invalidMin) {
					setStr += "<" + maxV;
				}
				holder.mSetValeuTV.setText(setStr);

				// 根据传感器的值来判断当前是否应该告警
				boolean isWarning = false;
				int value = itemInfo.getValue();
				if (value > invalidMin
						&& ((minV > invalidMin || maxV > invalidMin))) {
					if (minV > invalidMin && value < minV) {
						isWarning = true;
					}
					if (maxV > invalidMin && value > maxV) {
						isWarning = true;
					}
				}

				if (isWarning) {
					// 如果需要告警，背景则显示红色
					holder.mBgLayout.setBackgroundResource(R.color.card_bg_red);
					holder.mStautsTV.setText(R.string.warning);
				} else {
					// 不必告警则显示绿色
					holder.mBgLayout
							.setBackgroundResource(R.color.card_bg_green);
					holder.mStautsTV.setText(R.string.normal);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
}
